package cn.kimoon.dau_android;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.LinkedList;

import cn.kimoon.dau_android.Model.ModelCollectorInfo;
import cn.kimoon.dau_android.Model.ModelContactItem;

/**
 * Created by xiangdong on 2016/11/28.
 */

public class HumitureAdapter extends BaseAdapter {
    private ModelCollectorInfo info;
    private Context mContext;

    public HumitureAdapter(ModelCollectorInfo info, Context mContext) {
        this.info = info;
        this.mContext = mContext;
    }

    public void setData(ModelCollectorInfo info) {
        this.info = info;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return info == null ? 0 : info.temperature.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_humiture, parent, false);
            holder = new ViewHolder();
            holder.nameTextView = (TextView) convertView.findViewById(R.id.name_text_view);
            holder.valueTextView = (TextView) convertView.findViewById(R.id.value_text_view);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ModelContactItem item = info.temperature.get(position);
        System.out.println("名称" + item + "--name--");
        holder.nameTextView.setText(item.name);
        holder.valueTextView.setText((item.state == 0) ? mContext.getResources().getString(R.string.humiture_error) : String.format("%.1f℃", item.value / 10.0));
        if (item.state == 0 || item.value < info.temperature_secure_line_lower || item.value > info.temperature_secure_line_upper) {
            holder.valueTextView.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
        } else {
            holder.valueTextView.setTextColor(mContext.getResources().getColor(R.color.title_color));
        }

        return convertView;
    }

    static class ViewHolder {
        TextView nameTextView;
        TextView valueTextView;
    }
}
